﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;

namespace Rotempco.Core.Web.Logic
{
    public class DropDownListLogic
    {
        public static bool? GetBool(DropDownList dropDownList)
        {
            bool result;

            if (bool.TryParse(dropDownList.SelectedValue, out result))
                return result;

            return null;
        }

        public static TEnum? GetEnum<TEnum>(DropDownList dropDownList) where TEnum : struct
        {
            TEnum resultEnum;

            int selectedValue;
            if (int.TryParse(dropDownList.SelectedValue, out selectedValue) && selectedValue < 0)
                return null;

            if (Enum.TryParse<TEnum>(dropDownList.SelectedValue, out resultEnum))
                return resultEnum;

            return null;
        }

        public static int? GetInt(DropDownList dropDownList)
        {
            int result;

            if (int.TryParse(dropDownList.SelectedValue, out result) && result > 0)
                return result;

            return null;
        }
        
        public static long? GetLong(DropDownList dropDownList)
        {
            long result;

            if (long.TryParse(dropDownList.SelectedValue, out result) && result > 0)
                return result;

            return null;
        }
    }
}
